package com.yunxiao.cloud.web.service.impl;

import com.yunxiao.cloud.common.base.BaseResultResponse;
import com.yunxiao.cloud.web.service.OosDemoService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created on 2020/2/28 16:03
 *
 * @author shy
 * @date 2020-02-28
 * @since 11
 */
@Service
public class OosDemoServiceImpl implements OosDemoService {

    @Value("${oos.baseUrl}")
    private String baseUrl;

    @Value("${oos.request.path.uploadFile}")
    private String uploadFilePath;

    @Value("${oos.request.path.downloadFile}")
    private String downloadFilePath;

    @Override
    public BaseResultResponse upload(MultipartFile file) {
        RestTemplate template = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
        parts.add("file",file.getResource());
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(parts, headers);
        ResponseEntity<BaseResultResponse> responseEntity = template.postForEntity(baseUrl+uploadFilePath, httpEntity,BaseResultResponse.class);
        return responseEntity.getBody();
    }
}
